#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   App_SecurityAccess_0x27_test.py
@Time    :   2022/08/29 13:31:28
@Author  :   YangLiang 
@Version :   1.0
@Contact :   yangliang@chengtech.com
@License :   (C)Copyright 2022-2025
@Desc    :   None
'''

# here put the import lib
from pytest_assume.plugin import assume
 
import pytest
import logging
import time
import requests
from sympy import I
# from sympy import assuming
from commDriver.TS_CAN import *
import allure
import sys
from collections.abc import Iterable

from sqlalchemy import false
sys.dont_write_bytecode = True

SecurityAccess_Seed_NRC13_Extended = [  ([0x27],[0x7F,0x27,0x13]),             
                                        ([0x27,0x01,0x15],[0x7F,0x27,0x13]),
                                        ([0x27,0x01,0xFF],[0x7F,0x27,0x13])
                                   ]

SecurityAccess_Seed_NRC12_Extended = [  ([0x27,0x05,0x11],[0x7F,0x27,0x12]),
                                        ([0x27,0x03],[0x7F,0x27,0x12]), 
                                        ([0x27,0x04],[0x7F,0x27,0x12]),       
                                        ([0x27,0x05],[0x7F,0x27,0x12]),
                                        ([0x27,0x07],[0x7F,0x27,0x12])
                                   ]

SecurityAccess_Key_NRC13_Extended = [   ([0x27],[0x7F,0x27,0x13]),              
                                        ([0x27,0x02],[0x7F,0x27,0x13]),
                                        ([0x27,0x02,0xFF],[0x7F,0x27,0x13]),
                                        ([0x27,0x02,0x11,0x33],[0x7F,0x27,0x13]),
                                        ([0x27,0x02,0x11,0x33,0x22],[0x7F,0x27,0x13]),
                                        ([0x27,0x02,0x11,0x33,0x22,0x44,0x55],[0x7F,0x27,0x13])
                                   ]

SecurityAccess_Key_NRC12_Extended = [   ([0x27,0x04],[0x7F,0x27,0x12]),
                                        ([0x27,0x03],[0x7F,0x27,0x12]),         
                                        ([0x27,0x06],[0x7F,0x27,0x12]),
                                        ([0x27,0x08],[0x7F,0x27,0x12]),
                                        ([0x27,0x04,0x11,0x33,0x22,0x44],[0x7F,0x27,0x12])
                                   ]

SecurityAccess_NRC7F = [ ([0x27,0x01],[0x7F,0x27,0x7F]),
                         ([0x27,0x02],[0x7F,0x27,0x7F]),
                         ([0x27,0x03],[0x7F,0x27,0x7F]),
                         ([0x27,0x04],[0x7F,0x27,0x7F]),
                    ]


class Test_SecurityAccess_Phy(object):

     @allure.severity('normal')
     @allure.story('Tester Security Access')
     @allure.description('Test positive response of 0x27 seed in extended session with physical addressing')
     @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
     def test_SecurityAccess_Seed_Extended_Phy(self,action,Driver):
          iStep = 1
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x01], [0x67,0x01], 3, iStep, False)
          with assume: assert size == 6
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x01], [0x67,0x01], 0, iStep, False)
          with assume: assert size == 6

     @allure.severity('normal')
     @allure.story('Tester Security Access')
     @allure.description('Test positive response of 0x27 seed in programming session with physical addressing')
     @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
     def test_SecurityAccess_Seed_Programming_Phy(self,action,Driver):
          iStep = 1
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x03], [0x67,0x03], 2, iStep, False)
          with assume: assert size == 6
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x03], [0x67,0x03], 0, iStep, False)
          with assume: assert size == 6

     @allure.severity('minor')
     @allure.description('Test SecurityAccess Seed NRC of 0x27 seed in extended session with physical addressing')
     @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
     def test_SecurityAccess_Seed_NRC_Extended_Phy(self,action,Driver):
          iStep = 1
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x01], [0x7F,0x27,0x7F], 1, iStep, False)
          # 第一次
          logging.info("first answer NRC35")
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x01], [0x67,0x01], 3, iStep, False)
          with assume: assert size == 6
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x02,0x22,0x22,0x22,0x22], [0x7F,0x27,0x35], 0, iStep, False)
          # 第二次
          logging.info("second answer NRC35")
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x01], [0x67,0x01], 3, iStep, False)
          with assume: assert size == 6
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x02,0x22,0x22,0x22,0x22], [0x7F,0x27,0x35], 0, iStep, False)

          # 第三次
          logging.info("third answer NRC36")
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x01], [0x67,0x01], 3, iStep, False)
          with assume: assert size == 6
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x02,0x22,0x22,0x22,0x22], [0x7F,0x27,0x36], 0, iStep, False)
          time.sleep(2)

          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x3E,0x00], [0x7E,0x00], 0, iStep, False)
          logging.info("the first NRC37")
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x01], [0x7F,0x27,0x37], 0, iStep, False)
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x02,0x22,0x22,0x22,0x22], [0x7F,0x27,0x37], 0, iStep, False)
          time.sleep(2)

          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x3E,0x00], [0x7E,0x00], 0, iStep, False)
          logging.info("the two NRC37")
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x01], [0x7F,0x27,0x37], 0, iStep, False)
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x02,0x22,0x22,0x22,0x22], [0x7F,0x27,0x37], 0, iStep, False)
          time.sleep(2)

          
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x3E,0x00], [0x7E,0x00], 0, iStep, False)
          logging.info("the three NRC37")
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x01], [0x7F,0x27,0x37], 0, iStep, False)
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x02,0x22,0x22,0x22,0x22], [0x7F,0x27,0x37], 0, iStep, False)
          time.sleep(3)

          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x3E,0x00], [0x7E,0x00], 0, iStep, False)
          logging.info("the four NRC37")
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x01], [0x7F,0x27,0x37], 0, iStep, False)
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x02,0x22,0x22,0x22,0x22], [0x7F,0x27,0x37], 0, iStep, False)
          time.sleep(3)

          logging.info("the sid 0x27 normal")
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x3E,0x00], [0x7E,0x00], 0, iStep, False)
          iStep = Driver['CAN'].unlock(Driver, iStep)
          

     ids = ['Request {0}'.format(binascii.b2a_hex(bytearray(req),' ')) for req,resp in SecurityAccess_Seed_NRC13_Extended]
     @allure.severity('minor')
     @allure.description('Test NRC13 of 0x27 seed in extended session with physical addressing')
     @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
     @pytest.mark.parametrize('req,resp',SecurityAccess_Seed_NRC13_Extended,ids=ids)
     def test_SecurityAccess_Seed_NRC13_Extended_Phy(self,action,Driver,req,resp):
          iStep = 1
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False)

     ids = ['Request {0}'.format(binascii.b2a_hex(bytearray(req),' ')) for req,resp in SecurityAccess_Seed_NRC12_Extended]
     @allure.severity('minor')
     @allure.description('Test NRC12 of 0x27 seed in extended session with physical addressing')
     @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
     @pytest.mark.parametrize('req,resp',SecurityAccess_Seed_NRC12_Extended,ids=ids)
     def test_SecurityAccess_Seed_NRC12_Extended_Phy(self,action,Driver,req,resp):
          iStep = 1
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False)



     @allure.severity('normal')
     @allure.description('Test positive response of 0x27 key in extended session with physical addressing')
     @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
     def test_SecurityAccess_Key_Extended_Phy(self,action,Driver):
          iStep = 1
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x01], [0x67,0x01,0x00,0x00,0x00,0x00], 3, iStep, True)
          with assume: assert size == 6 


     @allure.severity('minor')
     @allure.description('Test NRC of 0x27 key in extended session with physical addressing')
     @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
     def test_SecurityAccess_Key_NRC_Extended_Phy(self,action,Driver):
          iStep = 1
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x02,0x11,0x11,0x11,0x11], [0x7F,0x27,0x24], 3, iStep, False)

          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x01], [0x67,0x01,0x00,0x00,0x00,0x00], 0, iStep, True)

          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x02,0x11,0x11,0x11,0x11], [0x7F,0x27,0x24], 3, iStep, False)

          iStep = Driver['CAN'].restartMRR_Power(Driver,iStep)
          # Test NRC35 of key of 0x27
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x01], [0x67,0x01], 3, iStep, False)
          with assume: assert size == 6
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x02,0x11,0x12,0x13,0x14], [0x7F,0x27,0x35], 0, iStep, False)

          # Test NRC35 of key of 0x27
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x01], [0x67,0x01], 3, iStep, False)
          with assume: assert size == 6
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x02,0x22,0x22,0x22,0x22], [0x7F,0x27,0x35], 0, iStep, False)

          # Test NRC36 of key of 0x27
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x01], [0x67,0x01], 3, iStep, False)
          with assume: assert size == 6
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x02,0x22,0x22,0x22,0x22], [0x7F,0x27,0x36], 0, iStep, False)
   

          for i in range(4):
               iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x01], [0x7F,0x27,0x37], 0, iStep, False)
               iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x02,0x22,0x22,0x22,0x22], [0x7F,0x27,0x37], 0, iStep, False)
               time.sleep(2)

          time.sleep(1)
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x3E,0x00], [0x7E,0x00], 0, iStep, False)
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x01], [0x7F,0x27,0x37], 0, iStep, False)
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x02,0x22,0x22,0x22,0x22], [0x7F,0x27,0x37], 0, iStep, False)

          time.sleep(2.5)
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x3E,0x00], [0x7E,0x00], 0, iStep, False)
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x01], [0x67,0x01], 3, iStep, False)
          with assume: assert size == 6
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x02,0x22,0x22,0x22,0x22], [0x7F,0x27,0x35], 0, iStep, False)



     ids = ['Request {0}'.format(binascii.b2a_hex(bytearray(req),' ')) for req,resp in SecurityAccess_Key_NRC13_Extended]
     @allure.severity('minor')
     @allure.description('Test NRC13 of 0x27 key in extended session with physical addressing')
     @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
     @pytest.mark.parametrize('req,resp',SecurityAccess_Key_NRC13_Extended,ids=ids)
     def test_SecurityAccess_Key_NRC13_Extended_Phy(self,action,Driver,req,resp):
          iStep = 1
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False)

     ids = ['Request {0}'.format(binascii.b2a_hex(bytearray(req),' ')) for req,resp in SecurityAccess_Key_NRC12_Extended]
     @allure.severity('minor')
     @allure.description('Test NRC12 of 0x27 key in extended session with physical addressing')
     @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
     @pytest.mark.parametrize('req,resp',SecurityAccess_Key_NRC12_Extended,ids=ids)
     def test_SecurityAccess_Key_NRC12_Extended_Phy(self,action,Driver,req,resp):
          iStep = 1
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False)



     @allure.severity('minor')
     @allure.description('Test delay response of 0x27 key after power on  in extended session with physical addressing')
     @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
     def test_SecurityAccess_delay_after_power_on_Phy(self,action,Driver):
          #Test ECU 上电,安全访问请求,收到正响应，无访问延时
          logging.info("Test delay response of 0x27 after ECU power on---start")
          iStep = 1
          seed = []
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x10,0x03], [0x50,0x03], 0, iStep, False)
          
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x01], [0x67,0x01], 0, iStep, False)
          with assume: assert size == 6
          for i in range(4):
               seed.append(uds_resp[2+i])
          key = Driver['CAN'].hangsheng_calcKey(seed)
          send_key = [0x27,0x02] + key
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, send_key, [0x67,0x02], 0, iStep, False)
 

     @allure.severity('minor')
     @allure.description('Test delay response of 0x27 key after reset ECU in extended session with physical addressing')
     @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
     def test_SecurityAccess_delay_after_reset_Phy(self,action,Driver):
          #Test ECU 复位后,安全访问请求,收到正响应，无访问延时
          logging.info("Test delay response of 0x27 after ECU reset---start")
          seed = []
          iStep = 1
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, "1101", "5101", 3, iStep, True)
          time.sleep(3)
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x01], [0x67,0x01], 3, iStep, False)
          with assume: assert size == 6
          for i in range(4):
               seed.append(uds_resp[2+i])
          key = Driver['CAN'].hangsheng_calcKey(seed)
          send_key = [0x27,0x02] + key
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, send_key, [0x67,0x02], 0, iStep, False)
  

     @allure.severity('minor')
     @allure.description('Test delay response of 0x27 key after SecurityAccess fail twice in extended session with physical addressing')
     @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
     def test_SecurityAccess_delay_after_failure_twice_Extended_Phy(self,action,Driver):
          #安全访问失败2次,安全访问请求,延时10s后安全访问收到正响应
          logging.info("Test delay response of 0x27 after SecurityAccess fail twice---start")
          time.sleep(0.5)
          iStep = 1
          seed = []
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x01], [0x67,0x01], 3, iStep, False)
          with assume: assert size == 6
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x02,0x11,0x11,0x11,0x11], [0x7F,0x27,0x35], 0, iStep, False)

          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x01], [0x67,0x01], 0, iStep, False)
          with assume: assert size == 6
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x02,0x11,0x11,0x11,0x11], [0x7F,0x27,0x35], 0, iStep, False)

          logging.info('wait 10s')
          time.sleep(10)

          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x01], [0x67,0x01], 3, iStep, False)
          with assume: assert size == 6
          for i in range(4):
               seed.append(uds_resp[2+i])
          
          key = Driver['CAN'].hangsheng_calcKey(seed)
          send_key = [0x27,0x02] + key
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, send_key, [0x67,0x02], 0, iStep, False)


     ids = ['Request {0}'.format(binascii.b2a_hex(bytearray(req),' ')) for req,resp in SecurityAccess_NRC7F]
     @allure.severity('minor')
     @allure.description('Test NRC7F of 0x27 in default session with physical addressing')
     @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
     @pytest.mark.parametrize('req,resp',SecurityAccess_NRC7F,ids=ids)
     def test_SecurityAccess_NRC7F_Default_Phy(self,action,Driver,req,resp):
          iStep = 1
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 1, iStep, False)


     @allure.severity('minor')
     @allure.description('Test NRC24 of 0x27 in extended session with physical addressing')
     @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
     def test_SecurityAccess_NRC24_Extended_Phy(self,action,Driver):
          iStep = 1
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '270211223344', '7F2724', 3, iStep, False)
